package com.dm.cloud.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.dm.cloud.auth.common.mode.AuthUserPermissionSaveModel;
import com.dm.cloud.auth.common.po.AuthRolePermission;
import com.dm.cloud.auth.service.AuthRolePermissionService;
import com.dm.cloud.basepo.service.SnowflakeGenId;
import com.dm.cloud.core.mode.UserMode;
import com.dm.cloud.core.mode.UserPermission;
import com.dm.cloud.core.service.user.CurrentUserService;
import com.dm.cloud.tkmybatis.service.BaseMapper;
import com.dm.cloud.auth.mapper.AuthPermissionMapper;
import com.dm.cloud.auth.service.AuthPermissionService;
import com.dm.cloud.auth.common.po.AuthPermission;
import com.dm.cloud.core.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.*;

/**
   * @author hcx
   * @type AuthPermissionServiceImpl.java
   * @desc AuthPermission serviceImpl
   */
@Service

@Transactional

public class AuthPermissionServiceImpl implements AuthPermissionService {


    @Autowired
    private AuthPermissionMapper authPermissionMapper;

    @Autowired
    private AuthRolePermissionService authRolePermissionService;

    @Autowired
    private CurrentUserService currentUserService;

    @Override
    public BaseMapper getMapper() {
        return authPermissionMapper;
    }
    public void processBeforeOperation(AuthPermission entity, BaseOperationEnum operationEnum) throws CustomException {
    }

    public void processAfterOperation(AuthPermission entity, BaseOperationEnum operationEnum) throws CustomException {
    }


    @Override
    public List<AuthPermission> findPermissionsByRoleIds(List<String> roleIds) {
        if(roleIds==null || roleIds.isEmpty()){
            return new ArrayList<>();
        }
        return authPermissionMapper.findByRoleIds(roleIds);
    }

    @Override
    public UserPermission findUserPermissionsByRoleIds(List<String> roleIds) {
        UserPermission userPermission=new UserPermission();
        if(roleIds==null || roleIds.isEmpty()){
            return userPermission;
        }
        List<AuthPermission> permissions= authPermissionMapper.findByRoleIds(roleIds);
        if(permissions!=null && !permissions.isEmpty()){
            List<UserPermission.FunctionPermissions> funcs = BeanUtil.copyToList(permissions, UserPermission.FunctionPermissions.class);
            userPermission.setFuncs(funcs);
        }
        return userPermission;
    }

    @Override
    public Integer saveRolePermission(AuthUserPermissionSaveModel saveModel) {
        if(StrUtil.isEmpty(saveModel.getRoleId())){
            throw new CustomException("角色Id不能为空！");
        }
        authRolePermissionService.delete(Example.builder(AuthRolePermission.class)
                .andWhere(Sqls.custom().andEqualTo("roleId",saveModel.getRoleId()))
                .build());
        Date curDate=new Date();
        UserMode currUser = currentUserService.getCurrentUser();
//        List<AuthRolePermission> rolePermissions=new ArrayList<>();
        int x=0;
        for (String permissionCode : saveModel.getPermissionCodes()) {
            if(StrUtil.isNotEmpty(permissionCode)){
//                rolePermissions.add(AuthRolePermission.builder()
//                        .id(SnowflakeGenId.genId())
//                        .permissionId(permissionCode)
//                        .roleId(saveModel.getRoleId())
//                        .createTime(curDate)
//                        .updateTime(curDate)
//                        .createUser(currUser.getUid())
//                        .updateUser(currUser.getUid())
//                        .build());
                x+= authRolePermissionService.insert(AuthRolePermission.builder()
                        .id(SnowflakeGenId.genId())
                        .permissionId(permissionCode)
                        .roleId(saveModel.getRoleId())
                        .createTime(curDate)
                        .updateTime(curDate)
                        .createUser(currUser.getUid())
                        .updateUser(currUser.getUid())
                        .build());
            }
        }
//        return authRolePermissionService.insertBatch(rolePermissions);
        return x;
    }

    @Override
    public List<String> getRolePermissions(String roleId) {
        List<String> rePermissions=new ArrayList<>();
        List<AuthPermission> permissions = findPermissionsByRoleIds(Arrays.asList(roleId));
        if(permissions!=null){
            for (AuthPermission permission : permissions) {
                rePermissions.add(permission.getCode());
            }
        }
        return rePermissions;
    }
}
